<template>
  <div class="ecmenu">
    <el-menu
      :default-active="currentMenu"
      :default-openeds="defaultList"
      class="el-menu-vertical-demo"
    >
      <el-submenu v-for="(item, index) in menuList" :key="index" :index="item.name">
        <template slot="title">
          <i :class="item.iconCls"></i>
          <span>{{ item.nameZh }}</span>
        </template>
        <el-menu-item
          v-for="subItem in item.children"
          :key="subItem.name"
          :index="subItem.name"
          @click="doChangePage(subItem)"
          >{{ subItem.nameZh }}</el-menu-item
        >
      </el-submenu>
    </el-menu>
  </div>
</template>

<script>
export default {
  name: "ecmenu",
  data() {
    return {
      menuList: [],
      defaultList: [],
      currentMenu: "",
    };
  },
  mounted() {
    let serviceKey = localStorage.getItem("serviceKey");
    this.$fetch({
      url: process.env.PROGOO_GATE_WATER + "menu/" + serviceKey,
      method: "get",
    }).then((res) => {
      this.menuList = res.data.result;
      this.currentMenu = this.$route.name;
      this.defaultList = [];
      for (let i in this.menuList) {
        this.defaultList.push(this.menuList[i].name);
      }
    });
  },
  watch: {
    "$route.name": {
      handler(newName, oldName) {
        this.currentMenu = newName;
      },
      immediate: true,
    },
  },
  methods: {
    doChangePage(item) {
      this.$router.push({ path: item.path });
    },
  },
};
</script>

<style lang="less">

@media screen and (min-width: 415px) {
.ecmenu {
  width: 200px;
  height: 100%;
  flex-shrink: 0;
  border-right: solid 1px #e6e6e6;
  overflow-y: scroll;
  overflow-x: hidden;
  .el-menu-vertical-demo {
    border: none;
  }
}
}
@media screen and (max-width: 414px) {
  .ecmenu {
    width: 100px;
    height: 100%;
    flex-shrink: 0;
    border-right: solid 1px #e6e6e6;
    overflow-y: scroll;
    overflow-x: hidden;
    .el-menu-vertical-demo {
      border: none;
      .el-submenu__title {
        padding: 0 10px !important;
        font-size: 12px;
      }
      .el-menu-item {
        padding: 0 10px !important;
        font-size: 12px;
      }
    }
  }
}
</style>
